package ca.llsutherland.nfl.persistence;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NamingException;

public abstract class AbstractDatabaseConnection implements DatabaseConnectionInterface {
	
	private Connection currentConnection;
    
    public abstract void initializeConnection() throws SQLException, NamingException;
    
    public void returnCurrentConnection() {
        try {
            this.currentConnection.close();
        } catch (SQLException e) {
            System.out.println("returnCurrentConnection() " + e.getMessage());
        } finally {
            this.currentConnection = null;
        }
    }

    public void rollback() throws SQLException {
        if (this.currentConnection != null) {
            this.currentConnection.rollback();
        }
    }

    public void commitTransaction() throws SQLException {
        if (this.currentConnection != null) {
            this.currentConnection.commit();
        }
    }

    public Connection getCurrentConnection() {
        return this.currentConnection;
    }

    public void setCurrentConnection(Connection currentConnection) {
        this.currentConnection = currentConnection;
    }

    public void isCurrentConnectionReadOnly(boolean isReadOnly) throws SQLException {
        if (this.currentConnection != null) {
            this.currentConnection.setReadOnly(isReadOnly);
        }
    }

    public void isCurrentConnectionAutoCommit(boolean isCurrentConnectionAutoCommit) throws SQLException {
        if (this.currentConnection != null) {
            this.currentConnection.setAutoCommit(isCurrentConnectionAutoCommit);
        }
    }

    public abstract void registerNewDomainObject(Long id, String tableName);
    
    public abstract void deleteNewDomainObjects();

    public abstract void removeDomainObject(Long idToRemove);
}
